package com.itheima.reggie.config;

import com.itheima.reggie.common.JacksonObjectMapper;
import com.itheima.reggie.common.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * 拦截器 ,用于拦截请求
 */
@Configuration //表示这个类是一个配置类
public class MyMvcConfig implements WebMvcConfigurer {

    //依赖注入,将LoginInterceptor拦截器注入进来
    @Autowired
    private LoginInterceptor loginInterceptor;

    //设置拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //添加拦截器
        registry.addInterceptor(loginInterceptor)
                //设置拦截路径
                //.addPathPatterns("/**")
                //排除不拦截的路径 (员工登录和静态页面)
                .excludePathPatterns("/employee/login", "/backend/**", "/front/**")
                //(用户验证页面和用户登录页面)
                .excludePathPatterns("/user/sendMsg","/user/login") ;
    }

    //扩展MVC的消息转换器
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        //1.创建一个消息转换器对象 MappingJackson2HttpMessageConverter
        MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
        //TODO 最核心的操作,设置核心对象 (不设置的话,默认是ObjectMapper)
        messageConverter.setObjectMapper(new JacksonObjectMapper());
        //2.将消息转换器对象添加到集合中(注意:需要放在集合的最前面,优先级高一些)
        converters.add(0, messageConverter);
    }
}
